/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.fjala.softure.tree.linkedTree;

import java.util.ArrayList;
import java.util.List;
import org.fjala.softure.tree.AbstractNode;
import org.fjala.softure.tree.Node;

/**
 * Linked Binary Node that implements methods of the AbstractNode class and contains references of
 * its children.
 * 
 * @author Luis Aguirre.
 * @author Juan Carlos Quispe.
 */
public class LinkedBinaryNode<T> extends AbstractNode<T> {
    
    private LinkedBinaryNode<T> left;
    
    private LinkedBinaryNode<T> right;
    
    public LinkedBinaryNode(T value, Node<T> parent) {
        super(value, parent);
    }

    public LinkedBinaryNode<T> getLeft() {
        return left;
    }

    public void setLeft(LinkedBinaryNode<T> left) {
        this.left = left;
    }

    public LinkedBinaryNode<T> getRight() {
        return right;
    }

    public void setRight(LinkedBinaryNode<T> right) {
        this.right = right;
    }

    @Override
    public List<Node<T>> getChildren() {
        List<Node<T>> list = new ArrayList<Node<T>>();
        if (left != null) {
            list.add(left);
        }
        if (right != null) {
            list.add(right);
        }
        return list;
    }

    @Override
    public boolean hasChildren() {
        return !(left == null && right == null);
    }

    @Override
    public void addChild(Node<T> child) {
        if (left == null) {
            child.setParent(this);
            left = (LinkedBinaryNode<T>) child;
        } else if (right == null) {
            child.setParent(this);
            right = (LinkedBinaryNode<T>) child;
        } else {
            if (left.getChildCount() <= right.getChildCount()) {
                left.addChild(child);
            }
            else {
                right.addChild(child);
            }
        }
    }
    
    @Override
    public int getChildCount() {
        if(left != null && right != null) {
            return 2;
        } else if(left == null && right == null) {
            return 0;
        }
        else {
            return 1;
        }
    }

    @Override
    public void removeChild(Node<T> child) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}